#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>



int g_sigstop = 0 ;

void Signal_stop(int signum)
{
	if(signum == SIGALRM)
	{
		printf("SIGALRM signal detected\n") ;
		g_sigstop = 1 ;

	}
	else if(signum == SIGINT)
	{
		printf("SIGINT signal get\n") ;
	}
	
}

void Signal_user(int signum)
{
	if(signum == SIGUSR1)
	{
		printf("sigaction get SIGUSR1\n") ;
	}
	else if(signum == SIGUSR2)
	{
		printf("SIGUSR2 sigaction geted\n") ;
	}

}

void signal_code(int signum)
{
	if(SIGBUS == signum)
	{
		printf("SIGBUS signal detected\n");
	}
	else if(SIGILL == signum)
	{
		printf("SIGILL signal detected\n");
	}
	else if(SIGSEGV == signum)
	{
		 printf("SIGSEGV signal detected\n");
	 }
		
	exit(-1);
}

int main (int argc, char **argv)
{
	char *ptr = NULL ;
	
	struct sigaction sig_act, sig_ign ;




	if ( signal(SIGINT, Signal_stop) == SIG_ERR)
	{
		printf("signal failed: %s\n", strerror(errno)) ;
		return -1 ;
	}
	
	if ( signal(SIGALRM, Signal_stop) == SIG_ERR )
	{
		printf("signal failed: %s\n", strerror(errno));
		return -2 ;
	}

	signal(SIGBUS, signal_code);
	signal(SIGILL, signal_code);
	signal(SIGSEGV, signal_code);



	/*Use function sigaction*/
	sigemptyset(&sig_act.sa_mask) ;
	sig_act.sa_flags = 0 ;
	sig_act.sa_handler = Signal_user ;
	
	sigemptyset(&sig_ign.sa_mask) ;
	sig_ign.sa_flags = 0 ;
	sig_ign.sa_handler = Signal_stop ;

	sigaction(SIGUSR1, &sig_act, NULL) ;
	sigaction(SIGUSR2, &sig_act, NULL) ;

	sigaction(SIGINT, &sig_ign, NULL) ;
	sigaction(SIGALRM, &sig_ign, NULL) ;




	while( !g_sigstop )
	{
		;
	}

	printf("Program start stop running...\n") ;


	printf("Invalid pointer operator will raise SIGSEGV signal\n") ;
	*ptr = 'h' ;

	
	
	





	return 0 ;
}
